﻿using Exam.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.BLL;
namespace ExaminationWeb.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        RoleService roleservice = new RoleService();
        public ActionResult RoleList()
        {
            return View();
        }
        public ActionResult RoleAdd()
        {
            return View();
        }
        public ActionResult RoleUpdate()
        {
            return View();
        }

        #region 数据请求
        public JsonResult RoleList_GetData()
        {
            int pageIndex = Request["sEcho"] == null ? 1 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }
            int totalcount = 0;
            string Rolename = Request["Rolename"] == null ? "" : Request["Rolename"];
            string isdelete = Request["isactive"] == null ? "-1" : Request["isactive"];
            IQueryable<Role> data;
            if (isdelete == "-1")
            {
                if (string.IsNullOrEmpty(Rolename))
                {
                    data = roleservice.LoadEntites(e => true, DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
                else
                {
                    data = roleservice.LoadEntites(e => e.RoleName.ToLower().Contains(Rolename.ToLower()), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(Rolename))
                {
                    //  data = toolListService.LoadEntites(e => e.State == 0 , pageStart, pageSize, out totalcount);
                    data = roleservice.LoadEntites(e => e.IsActive == int.Parse(isdelete), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
                else
                {
                    data = roleservice.LoadEntites(e => e.IsActive == int.Parse(isdelete) && e.RoleName.ToLower().Contains(Rolename.ToLower()), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
            }
            var result = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = data, sEcho = 0 };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
